﻿using System;
using System.Collections.Generic;
////using System.Linq;
using System.Text;
using DBConnectionLib;
using System.Collections.Specialized;

namespace DBSourceToolsLib
{
    public class DatabaseCreateScript : DatabaseScript
    {
        // TODO : parse create script for invalid path names. (albTotxo)
        public DatabaseCreateScript(DatabaseEx oParentDatabase, DatabaseEx oSourceDatabase)
            : base(oParentDatabase, oSourceDatabase)
        {
            this.ScriptFilePath = oParentDatabase.BaseDirectory + "\\" + oParentDatabase.DatabaseName + "_CreateDB.sql";
            base.LoadScriptFromDisk();
        }
        public override void  WriteDefaultScript()
        {
            if (this.m_SourceDatabase != null)
            {
                try
                {
                    ScriptCreateFromSourceDatabase();
                }
                catch (Exception ex)
                {
                    ScriptCreateFromStandard();
                }
            }


        }

        private void ScriptCreateFromStandard()
        {
            //throw new NotImplementedException();
            base.ScriptText = String.Format(@"
CREATE DATABASE [{0}] ON  PRIMARY 
( NAME = N'{0}', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\{0}.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 2400KB )
 LOG ON 
( NAME = N'{0}_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\{0}.ldf' , SIZE = 3072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 COLLATE Latin1_General_CI_AI
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'{0}', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [{0}].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO
ALTER DATABASE [{0}] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [{0}] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [{0}] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [{0}] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [{0}] SET ARITHABORT OFF 
GO
ALTER DATABASE [{0}] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [{0}] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [{0}] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [{0}] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [{0}] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [{0}] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [{0}] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [{0}] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [{0}] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [{0}] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [{0}] SET  DISABLE_BROKER 
GO
ALTER DATABASE [{0}] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [{0}] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [{0}] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [{0}] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [{0}] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [{0}] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [{0}] SET  READ_WRITE 
GO
ALTER DATABASE [{0}] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [{0}] SET  MULTI_USER 
GO
ALTER DATABASE [{0}] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [{0}] SET DB_CHAINING OFF 
GO
", this.m_SourceDatabase.DatabaseName);
            base.WriteScript();
        }

        private void ScriptCreateFromSourceDatabase()
        {
            Microsoft.SqlServer.Management.Smo.Server oServer = this.m_SourceDatabase.GetSmoServer();
            Microsoft.SqlServer.Management.Smo.Database oDatabase = this.m_SourceDatabase.GetSmoDatabase(oServer);
            Microsoft.SqlServer.Management.Smo.ScriptingOptions oScriptingOptions = new Microsoft.SqlServer.Management.Smo.ScriptingOptions();
            oScriptingOptions.ExtendedProperties = UserSettings.Instance().UseExtendedProperties;
            oScriptingOptions.AnsiFile = UserSettings.Instance().UseANSII;

            StringCollection myStrColl = new StringCollection();
            oScriptingOptions.FileName = this.ScriptFilePath;
            myStrColl = oDatabase.Script(oScriptingOptions);
        }

    }
}
